package dev.loong.boot3.controller;

import dev.loong.boot3.service.PurchaseService;
import dev.loong.boot3.vo.ResultVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/users/purchase")
public class PurchaseController {
    private final PurchaseService service;
    public PurchaseController(PurchaseService service) {
        this.service = service;
    }

    @GetMapping("/test")
    public ModelAndView testPage() {
        return new ModelAndView("purchase/test");
    }
    @PostMapping
    public ResultVO<?> purchase(Long userId, Long productId, int quantity) {
        var success = service.purchase(userId, productId, quantity);
        var message = success ? "抢购成功" : "抢购失败";
        return new ResultVO<>(success, message);
    }
}
